package com.enzo.prms.common.listener;

import java.util.Timer;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class AuditListener implements ServletContextListener {
	private final Logger logger=Logger.getLogger(AuditListener.class);
	private Timer timer;
	
	@Override
	public void contextInitialized(ServletContextEvent event) {
		ServletContext servletContext=event.getServletContext();
		ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);
		MyTask myTask = (MyTask) ctx.getBean("myTask");
		timer=new Timer(true);
		timer.schedule(myTask, 0,30*60*1000);
	}

	@Override
	public void contextDestroyed(ServletContextEvent event) {
		logger.info(">>>>>>> AuditListener contextDestroyed <<<<<<<<");
		timer.cancel();
	}
}
